/*
 * File: FacesUtil.java
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package tvseries.utils;

import java.util.ArrayList;
import java.util.Map;

import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 * Generic utility class for JSF.
 * 
 * @author nish.naidoo@gmail.com
 *
 */
public class FacesUtil
{
	public static void addMessage(String id, String message)
	{
		FacesContext facesContext = FacesContext.getCurrentInstance();
		FacesMessage facesMessage = new FacesMessage(message);
		facesContext.addMessage(id, facesMessage);
	}
	
	public static String getRequestParameter(String param)
	{
		FacesContext ctx = FacesContext.getCurrentInstance();
		if (ctx == null)
			return null;
		
		Map<?,  ?> params = ctx.getExternalContext().getRequestParameterMap();
		String paramValue = (String) params.get(param);
		
		return paramValue;
	}
	
	public static Object getBean(String beanName)
	{
		FacesContext ctx = FacesContext.getCurrentInstance();
		if (ctx == null)
			return null;
		
		Application app = ctx.getApplication();
		Object bean = app.createValueBinding("#{" + beanName + "}").getValue(ctx);

		return bean;
	}
	
	public static void setBean(String beanName, Object bean)
	{
		FacesContext ctx = FacesContext.getCurrentInstance();
		if (ctx == null)
			return;
		
		Application app = ctx.getApplication();
		app.createValueBinding("#{" + beanName + "}").setValue(ctx, bean);
	}
	
	/**
	 * This function takes a string and breaks them into tokens that are used
	 * for searching.
	 * @param str This is a string containing the search terms that are required
	 * to parse.
	 * @return returns a String array containing search tokens.
	 */
	public static String[] makeTermArray(String searchString)
	{
		// add a space at the end of the string for
		// the parser to catch a word.
		String str = new String(searchString + " ");

		char chars[] = str.toLowerCase().toCharArray();
		boolean hitspace = false;
		boolean openquote = false;
		ArrayList<String> terms = new ArrayList<String>();
		StringBuffer sb = new StringBuffer(20);

		for (int x = 0; x < chars.length; x++)
		{
			if (chars[x] == ' ' && hitspace == false && openquote == false)
			{
				hitspace = true;
				if (sb.length() > 0)
				{
					terms.add(sb.toString());
					sb = new StringBuffer(20);
				}
			}
			else if (chars[x] == '"' && openquote == false)
			{
				hitspace = false;
				openquote = true;
			}
			else if (chars[x] == '"' && openquote == true)
			{
				hitspace = true;
				openquote = false;
				if (sb.length() > 0)
				{
					terms.add(sb.toString());
					sb = new StringBuffer(20);
				}
			}
			else if (!(chars[x] == ' ' && hitspace == true && openquote == false))
			{
				hitspace = false;
				sb.append(chars[x]);
			}
		}

		return terms.toArray(new String[terms.size()]);
	}
}
